package cn.tannn;


import cn.tannn.sink.MysqlSink;
import com.ververica.cdc.connectors.mysql.source.MySqlSource;
import com.ververica.cdc.debezium.JsonDebeziumDeserializationSchema;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author tn
 * @version 1
 * @date 2022-02-06 21:34
 */
public class FlinkCDC {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.enableCheckpointing(3000);
//        读取mysqlbinlog
        MySqlSource<String> mySqlSource = MySqlSource.<String>builder()
                .hostname("192.168.0.99")
                .port(3306)
                .databaseList("flink") // set captured database
                .tableList("flink.base_trademark") // set captured table
                .username("root")
                .password("root")
                .deserializer(new JsonDebeziumDeserializationSchema()) // converts SourceRecord to JSON String
                .build();
        DataStreamSource<String> mySQL_source = env.fromSource(mySqlSource, WatermarkStrategy.noWatermarks(), "MySQL Source");

//        env
//                .fromSource(mySqlSource, WatermarkStrategy.noWatermarks(), "MySQL Source")
//                // set 4 parallel source tasks
//                .setParallelism(4)
//                .print().setParallelism(1); // use parallelism 1 for sink to keep message ordering

        // sink操作
        mySQL_source.print();
        mySQL_source.addSink(new MysqlSink());
        env.execute("mysql-mysql-cdc");
    }
}
